﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-14
Description:MenuList.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FineUI;
using FTchina.Logic;
using FTchina.Library;
using FTchina;
using FTchina.DAL;
public partial class MenuListaction  : BasePage
{


    bool Isdpt = false;//默认添加为子项。不是菜单

    protected void Page_Load(object sender, EventArgs e)
    {

        Isdpt = RequestHelper.SafeRequest(Request["Isdpt"]) == "1";

        if (!IsPostBack)
        {
            LoadData();
            BindDropDownList();
            string id = RequestHelper.SafeRequest(Request["id"]);
            if (id.Length > 0)
            {
                ShowModData(id);
            }
        }

    }
    /// <summary>
    /// 绑定级别的下拉列表
    /// </summary>
    private void BindDropDownList()
    {
        DDLParentId.DataTextField = "Name";
        DDLParentId.DataValueField = "Id";
        DDLParentId.DataSimulateTreeLevelField = "Level";
        DDLParentId.DataEnableSelectField = "EnableSelect";
        DDLParentId.DataSource = MenuListLogic.getDropDownListTree();
        DDLParentId.DataBind();
        DDLParentId.Items.Add(new FineUI.ListItem("根菜单", "RootMenu"));
        DDLParentId.SelectedIndex = DDLParentId.Items.Count - 1;


        CkbPower.DataSource = DBHelper.GetDataTable("select * from usergroup ");
        CkbPower.DataTextField = "groupname";
        CkbPower.DataValueField = "id";
        CkbPower.DataBind();
        CkbPower.SelectedValueArray = new string[] { "1" };//只有这样才能获取选择的项
    }
    private void LoadData()
    {

        // 关闭按钮的客户端脚本：
        // 1. 首先确认窗体中表单是否被修改（如果已经被修改，则弹出是否关闭的确认对话框）
        // 2. 然后关闭本窗体，回发父窗体
        btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();

    }
    private void ShowModData(string ItemID)
    {
        FTchina.Models.MenuList mMenuList = FTchina.BLL.MenuListManager.GetMenuListById(Convert.ToInt32(ItemID));
        if (mMenuList != null)
        {
            txbid.Text = mMenuList.Id.ToString();

            txbconText.Text = mMenuList.ConText.ToString();
            txblink.Text = mMenuList.Link.ToString();
            txbpaixu.Text = mMenuList.Paixu.ToString();

            CkbPower.SelectedValueArray = mMenuList.Power.Split(',');
            if (MenuListLogic.IsRootItem(ItemID))
            {
                DDLParentId.SelectedValue = "RootMenu";
            }
            else
            {
                DDLParentId.SelectedValue = mMenuList.ParentID.ToString();
            }
        }
        else
        {
            AlertLogic.ShowResultAndRedirect("无数据！", "提示", HttpContext.Current.Request.Path.ToString(), false);
        }
    }
    protected void btnSaveContinue_Click(object sender, EventArgs e)
    {
        if (txbid.Text == DDLParentId.SelectedValue)
        {
            AlertLogic.ShowError("不能将自身作为父级菜单，请重新选择");
            return;
        }
        bool AddNew = RequestHelper.SafeRequest(Request["id"]).Length == 0 ? true : false;//设置是否新增
        if (DDLParentId.SelectedValue == "RootMenu")
        {//根菜单类型
            if (AddNew)
            {//新增项目

                FTchina.Models.MenuList mMenuList = new FTchina.Models.MenuList();
                mMenuList.ConText = txbconText.Text.Trim();
                mMenuList.TreeLevel = "1";
                mMenuList.Link = "";
                mMenuList.IsDpt = Isdpt ? "1" : "0";

                AlertLogic.ShowResult(FTchina.BLL.MenuListManager.AddOrUpdateMenuList(mMenuList) > 0);
            }
            else
            {

                FTchina.Models.MenuList mMenuList = new FTchina.Models.MenuList();
                mMenuList.Id = Convert.ToInt32(this.txbid.Text.Trim());
                mMenuList.ConText = txbconText.Text.Trim();
                mMenuList.TreeLevel = "1";
                mMenuList.Paixu = Convert.ToDecimal(txbpaixu.Text);
                mMenuList.Power = StringHelper.getArrayString(CkbPower.SelectedValueArray);
                mMenuList.IsDpt = Isdpt ? "1" : "0";

                AlertLogic.ShowResult(FTchina.BLL.MenuListManager.AddOrUpdateMenuList(mMenuList) > 0);
            }
        }
        else
        {//普通菜单

            FTchina.Models.MenuList mMenuList = new FTchina.Models.MenuList();
            mMenuList.ConText = txbconText.Text.Trim();
            mMenuList.Id = txbid.Text.Trim() == "" ? 0 : Convert.ToInt32(txbid.Text);
            mMenuList.ParentID = Convert.ToInt32(this.DDLParentId.SelectedValue);
            mMenuList.IsDpt = Isdpt ? "1" : "0";

            mMenuList.Power = StringHelper.getArrayString(CkbPower.SelectedValueArray);
            mMenuList.Paixu = Convert.ToDecimal(txbpaixu.Text.Trim());
            mMenuList.Link = txblink.Text.Trim();
            int x = 2;
            MenuListLogic.getTreeLevel(mMenuList.ParentID.ToString(), ref x);
            mMenuList.TreeLevel = x.ToString();
            AlertLogic.ShowResult(FTchina.BLL.MenuListManager.AddOrUpdateMenuList(mMenuList) > 0);
        }
        if (!AddNew)
        {//修改已有项目
            //重新计算所有IDs的treeLevel
            MenuListLogic.setReferMenuIdsTreeLevel(txbid.Text.Trim());
        }


        // 1. 这里放置保存窗体中数据的逻辑


        // 2. 关闭本窗体，然后回发父窗体
        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
    }
    //protected void btnSaveRefresh_Click(object sender, EventArgs e)
    //{
    //    // 1. 这里放置保存窗体中数据的逻辑

    //    // 2. 关闭本窗体，然后刷新父窗体
    //    PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
    //}






}






